package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.AcademyEntity;
import com.tfjybj.itoo.teach.model.AcademyModel;
import com.tfjybj.itoo.teach.provider.service.AcademyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * AcademyController
 * academy表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"academy表接口"})
@RequestMapping(value = "/academy")
@RestController
public class AcademyController {

    @Resource
    private AcademyService academyService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model AcademyModel
     * @return 添加的结果（修改，加入判重，设置默认type）
     * @author 杨燕梅
     * @since 2.0.0 2018-12-20 14:13:09
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    @Transactional(rollbackFor = Exception.class)
    public ItooResult create(@RequestBody AcademyModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        //判断教学部门是否重复
        if (!CollectionUtils.isEmpty(academyService.queryByCodeAndNameExist(model.getCode(),model.getName()))){
            return ItooResult.build(ItooResult.FAIL, "教学部门已存在");
        }
        AcademyEntity academyEntity = new AcademyEntity();
        BeanUtils.copyProperties(model, academyEntity);
        academyEntity.setType(1);
        academyService.save(academyEntity);
        academyService.saveToAuth(academyEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        academyService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        academyService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model AcademyModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改academy")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody AcademyModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        AcademyEntity academyEntity = new AcademyEntity();
        BeanUtils.copyProperties(model, academyEntity);
        academyService.updateById(academyEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Academy
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        AcademyEntity academyEntity = academyService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyEntity);
    }

    /**
     * 分页查询所有Academy
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有Academy")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<AcademyEntity> academys = academyService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academys);
    }

    /**
     * 根据教学部门名称模糊查询academy
     * @param name  教学部门名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的academy
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据教学部门名称模糊查询academy", notes = "分页根据教学部门名称模糊查询academy")
    @GetMapping(value = "queryAcademyByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryAcademyByLikeName(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                             @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize,
                                             @RequestParam(required = false, defaultValue = "") String name) {
        PageInfo<AcademyEntity> academyList = academyService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 根据学院或者机构id查询学院或机构名称
     * @param ids 学院或者机构id
     * @return 查询的学院或者机构名称
     * @author 杨燕梅
     * @since 2.0.0 2018-11-17 14:22:55
     */
    @ApiOperation(value="根据学院或者机构id查询学院或者机构名称", notes = "根据学院或者机构id查询学院或者机构名称")
    @PostMapping(value = {"/queryAcadOrInstInfoByAcadIdOrInstId/{ids}"})
    public ItooResult queryAcadOrInstInfoByAcadIdOrInstId(@ApiParam(name = "ids", value = "学院id或机构id", required = true)@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ItooResult.build(ItooResult.FAIL, "学院或机构id为空！");
        } else {
            List<AcademyModel> academyOrInstitutionList = academyService.queryInfoByAcadIdOrInstId(ids);
            return ItooResult.build(ItooResult.SUCCESS,"查询学院或机构名称成功！",academyOrInstitutionList);
        }
    }

    /**
     * 查询所有学院或者机构名称
     * @return 所有的学院或者机构名称
     * @author 杨燕梅
     * @since 2.0.0 2018-11-17 21:25:37
     */
    @ApiOperation(value="查询所有学院或者机构名称", notes = "查询所有学院或者机构名称")
    @PostMapping(value = "/queryAcademyOrInstitutionInfo")
    public ItooResult queryAcademyOrInstitutionInfo(){
        List<AcademyModel> academyOrInstitutionList=academyService.queryAcadOrInstInfo();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyOrInstitutionList);
    }

    /**
     * 查询所有教学部门
     * @author 杨燕梅
     * @since 2.0.0 2018-11-23 11:52:12
     */
    @ApiOperation(value = "查询所有教学部门", notes = "查询所有教学部门")
    @GetMapping(value = {"/queryAllAcademy"})
    public ItooResult queryAllAcademy() {
        List<AcademyModel> academyList = academyService.queryAllAcademy();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyList);
    }

    /**
     * 查询所有教学部门（带分页）
     * @author 杨燕梅
     * @since 2.0.0 2018-12-19 15:17:30
     */
    @ApiOperation(value = "查询所有教学部门（带分页）", notes = "查询所有教学部门（带分页）")
    @GetMapping(value = "queryAllAcademyPageInfo/{pageNo}/{pageSize}")
    public ItooResult queryAllAcademyPageInfo(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
                                              @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize) {
        PageInfo<AcademyModel> academyList = academyService.queryAllAcademyPageInfo(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyList);
    }

    //导出
    /**
     * 根据教学部门名称或教学部门简称模糊查询academy(带分页)
     * @param searchContent  教学部门名称/教学部门简称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的academy
     * @author 杨燕梅
     * @since 2.0.0 2018-12-6 21:24:36
     */
    @ApiOperation(value = "根据教学部门名称或教学部门简称模糊查询academy(带分页)", notes = "分页根据教学部门名称或教学部门简称模糊查询academy")
    @GetMapping(value = "queryAcademyByLikeNameOrAbbreviation/{pageNo}/{pageSize}")
    public ItooResult queryAcademyByLikeNameOrAbbreviation(@RequestParam(required = false, defaultValue = "") String searchContent,
                                                           @ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
                                                           @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize) {
        PageInfo<AcademyModel> academyList = academyService.queryByLikeNameOrAbbreviation(searchContent, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyList);
    }

    /**
     * 根据教学部门ID查询教学部门详情
     *
     * @param idList 教学部门ID
     * @return 教学部门实体集合
     * @author 杨燕梅
     * @since 2.0.0 2018-12-11 10:14:26
     */
    @ApiOperation(value = "根据教学部门ID查询教学部门详情", notes = "根据教学部门ID查询教学部门详情")
    @GetMapping(value = "/queryAcademyById/{idList}")
    public ItooResult queryAcademyById(@ApiParam(name = "idList", value = "教学部门ID", required = true) @PathVariable List<String> idList) {
        List<AcademyModel> academyModelList =academyService.queryAcademyById(idList);
        return ItooResult.build(ItooResult.SUCCESS, "查询教学部门成功!", academyModelList);
    }

    /**
     * 根据ID导出教学部门信息
     *
     * @param response 响应
     * @param idList   教学部门ID
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-11 09:45:16
     */
    @ApiOperation(value = "根据ID导出教学部门信息", notes = "根据ID导出教学部门信息")
    @GetMapping(value = {"/exportAll"})
    public ItooResult exportAll(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") List<String> idList) {
        return academyService.exportByAcademyId(response, idList);
    }

    /**
     * 根据条件导出教学部门
     *
     * @param response     响应
     * @param searchContent    教学部门名称/教学部门简称
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-13 08:11:09
     */
    @ApiOperation(value = "根据条件导出教学部门", notes = "根据条件导出教学部门")
    @GetMapping(value = {"/exportByStrLike"})
    public ItooResult exportByStrLike(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") String searchContent) {
        return academyService.exportByStrLike(response,searchContent);
    }

    //导入
    /**
     * 下载教学部门模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 杨燕梅
     * @since 2.0.0 2018-11-29 15:40:32
     */
    @ApiOperation(value = "下载教学部门模板，批量导入使用", notes = "下载教学部门模板，批量导入使用")
    @GetMapping(value = {"/downLoadAcademyTemplate"})
    public ItooResult downLoadAcademyTemplate(HttpServletResponse response) {
        if (academyService.downLoadTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载教学部门模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载教学部门模板失败");
        }
    }

    /**
     * 批量导入教学部门
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 杨燕梅
     * @since 2.0.0 2018-11-29 15:41:22
     */
    @ApiOperation(value = "批量导入教学部门", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importAcademyInfoByTemplate"})
    public ItooResult importAcademyInfoByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                                  HttpServletRequest request, HttpServletResponse response) {
        return academyService.importTemplate(multipartFile, request, response);
    }

    /**
     * 根据errorListId导出未成功导入的信息
     *
     * @param response    响应
     * @param errorListId 为成功导入的分组id
     * @return 导出结果
     * @author 杨燕梅
     * @since 1.0.0 2018-12-1 19:39:17
     */
    @ApiOperation(value = "根据errorListId导出未成功导入的信息",notes = "根据errorListId导出未成功导入的信息")
    @GetMapping(value = {"/exportErrorAcademyById/{errorListId}"})
    public ItooResult exportErrorAcademyById(HttpServletResponse response,
                                             @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {
        if (academyService.exportErrorList(errorListId, response)) {
            return ItooResult.build(ItooResult.SUCCESS, "导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "导出失败");
        }
    }

    /**
     * 根据教学部门代码和教学部门名称查询教学部门是否已存在
     *
     * @param code   教学部门代码
     * @param name   教学部门名称
     * @return 教学部门信息
     * @author 杨燕梅
     * @since 2.0.0 2018-11-29 19:37:30
     */
    @ApiOperation(value = "根据教学部门代码和教学部门名称查询教学部门是否已存在", notes = "根据教学部门代码和教学部门名称查询教学部门是否已存在")
    @GetMapping(value = {"/queryByCodeAndNameExist/{code}/{name}"})
    public ItooResult queryByCodeAndNameExist(@ApiParam(name = "code", value = "教学部门代码", required = true) @PathVariable String code,
                                              @ApiParam(name = "name", value = "教学部门名称", required = true) @PathVariable String name) {
        List<AcademyEntity> academyList = academyService.queryByCodeAndNameExist(code,name);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyList);
    }

}



